maltes
committed
on 22 Sep 05
Fixing tasks page. Adding missing variables to the include. Got rid of the differentiation between watcher_p and observer_p, added a proper … Show more
Fixing tasks page. Adding missing variables to the include. Got rid of the differentiation between watcher_p and observer_p, added a proper filter for the party_id, removed the role_id issue, added the option to pass a filter for package_id, allowing you to filter the projects to only the projects in this subsite

Show less

openacs-4/.../simulation/tcl/template-procs.tcl (+102 -12)
432 432 } {
433 433     Dissociate an object with a simulation template
434 434 } {
435 435     db_dml remove_object_from_workflow_delete {
436 436             delete from sim_workflow_object_map
437 437             where workflow_id =  :template_id
438 438               and object_id = :object_id
439 439     }
440 440     # no special error handling because the delete is pretty safe
441 441 }
442 442
443 443 ad_proc -public simulation::template::force_start {
444 444      {-workflow_id:required}
445 445 } {
446 446     Force a simulation to start immediately by updating case_start,
447 447     enroll_end, and enroll_start properties to reflect an immediate start
448 448     and then directly invoking simulation::template::start.
449 449
450 450     @author Peter Marklund
451 451 } {
452       # TODO (.25h): make sure the sweeper doesn't pick up simulations that have been forced to start.
453  
454 452     simulation::template::get -workflow_id $workflow_id -array simulation
455 453
456 454     db_transaction {
457 455         # Move enroll_end to now if it's in the future
458 456         set today [db_string select_today {
459 457             select to_char(current_timestamp, 'YYYY-MM-DD')
460 458         }]
461 459         if { [clock scan $today] < [clock scan $simulation(enroll_end)] } {
462 460             set simulation_edit(enroll_end) $today
463 461         }
464 462         # enroll_start must be before or equal enroll_end
465 463         if { [clock scan $today] < [clock scan $simulation(enroll_start)] } {
466 464             set simulation_edit(enroll_start) $today
467 465         }
468 466
469 467         # Set start_date to now
470 468         set simulation_edit(case_start) $today
471 469
472 470         simulation::template::edit -workflow_id $workflow_id -array simulation_edit
473 471
 
485 483 } {
486 484     set simulation_edit(enrolled) [list]
487 485     set enroll_email_list [list]
488 486     set invite_email_list [list]
489 487     db_foreach select_enrolled_and_invited_users {
490 488             select distinct pamm.member_id as user_id,
491 489                    cu.email,
492 490                    cu.first_names || ' ' || cu.last_name as user_name,
493 491                    spsm.type
494 492             from sim_party_sim_map spsm,
495 493                  party_approved_member_map pamm,
496 494                  cc_users cu
497 495             where spsm.simulation_id = :workflow_id
498 496              and (spsm.type = 'auto_enroll' or spsm.type = 'invited')
499 497              and pamm.party_id = spsm.party_id
500 498              and pamm.member_id = cu.user_id
501 499              and pamm.party_id <> pamm.member_id
502 500     } {
503 501         if { [string equal $type "auto_enroll"] } {
504 502             # enroll the user automatically
505               lappend $simulation_edit(enrolled) $user_id
  503             lappend simulation_edit(enrolled) $user_id
506 504             lappend enroll_email_list [list $email $user_name]
507 505         } else {
508 506             # Invite the user
509 507             lappend invite_email_list [list $email $user_name]           
510 508         }
511 509     }
512 510
513       # Remove duplicates
514       set simulation_edit(enrolled) [lsort -unique $simulation_edit(enrolled)]
515 511     simulation::template::edit -workflow_id $workflow_id -array simulation_edit
516 512
517 513     simulation::template::get -workflow_id $workflow_id -array sim_template
518 514
519 515     if { [string equal $sim_template(casting_type) "open"] || [string equal $sim_template(casting_type) "group"] } {
520 516         # Notify users that they are enrolled and can do their casting
521 517         foreach user $enroll_email_list {
522 518             set email [lindex $user 0]
523 519             set user_name [lindex $user 1]
524 520
525 521             set subject "You have been enrolled in simulation $sim_template(pretty_name)"
526 522             # TODO: check that link to casting page is correct
527 523             set package_id [ad_conn package_id]
528 524             set casting_page_url \
529 525                 [export_vars -base "[ad_url][apm_package_url_from_id $package_id]/simplay/enroll" { email }]
530 526             set body "Dear $user_name,
531 527 This is to notify you that you have been enrolled in simulation $sim_template(pretty_name). You may visit the
532 528 casting page at ${casting_page_url}.
533 529 "
534 530
535 531             acs_mail_lite::send \
536 532                 -to_addr $email \
537 533                 -from_addr [ad_system_owner] \
538 534                 -subject $subject\
539 535                 -body $body
540 536         }
541 537     }
542 538
543 539     # Invite users
544 540     foreach user $invite_email_list {
545 541         set email [lindex $user 0]
546 542         set user_name [lindex $user 1]       
547 543
548 544         # TODO: check that link to enrollment page is correct
549 545         set package_id [ad_conn package_id]
550 546         set enrollment_page_url \
551 547             [export_vars -base "[ad_url][apm_package_url_from_id $package_id]/simplay/enroll" { email }]
552 548         set subject "You have been invited to join simulation $sim_template(pretty_name)"
553 549         set body "Dear $user_name,
554 550 You have been invited to join simulation $sim_template(pretty_name). Please visit the enrollment page at $enrollment_page_url to accept the invitation. Thank you!"
  551         acs_mail_lite::send \
  552             -to_addr $email \
  553             -from_addr [ad_system_owner] \
  554             -subject $subject\
  555             -body $body
  556     }
  557 }
555 558
  559 ad_proc -private simulation::template::sweeper {} {
  560     Starts simulations and sends notifications.
  561
  562     @author Peter Marklund
  563 } {
  564     # Make simulations go live
  565     set simulations_to_start [db_list select_simulations_to_start {
  566         select simulation_id
  567         from sim_simulations
  568         where sim_type <> 'live_sim'
  569         and case_start < current_timestamp
  570     }]   
  571     foreach simulation_id $simulations_to_start {
  572         ns_log Notice "pm debug starting simulation $simulation_id"
  573         start -workflow_id $simulation_id
  574     }   
  575
  576     # For simulations that are not live yet and have reached their send_start_note_date,
  577     # send notifications to users in simulations that have not already been emailed.
  578     set users_to_notify [db_list_of_lists select_simulations_to_start {
  579         select cu.user_id,
  580                cu.email,
  581                cu.first_names || ' ' || cu.last_name as user_name,
  582                ss.simulation_id,
  583                w.pretty_name as simulation_name,
  584                to_char(ss.case_start, 'YYYY-MM-DD') as simulation_start_date,
  585                w.description as simulation_description
  586         from sim_simulations ss,
  587              workflows w,
  588              sim_party_sim_map spsm,
  589              cc_users cu
  590         where sim_type <> 'live_sim'
  591         and ss.simulation_id = spsm.simulation_id
  592         and ss.simulation_id = w.workflow_id
  593         and spsm.type = 'enrolled'
  594         and cu.user_id = spsm.party_id
  595         and ss.send_start_note_date < current_timestamp
  596         and not exists (select 1
  597                         from sim_simulation_emails sse
  598                         where sse.simulation_id = ss.simulation_id
  599                           and sse.user_id = spsm.party_id
  600                           and sse.email_type = 'reminder')       
  601     }]   
  602     foreach row $users_to_notify {
  603         set user_id [lindex $row 0]       
  604         set email [lindex $row 1]
  605         set user_name [lindex $row 2]
  606         set simulation_id [lindex $row 3]
  607         set simulation_name [lindex $row 4]
  608         set simulation_start_date [lindex $row 5]
  609         set simulation_description [lindex $row 6]
  610
  611         set subject "Simulation $simulation_name starts on $simulation_start_date"
  612         set body "Dear $user_name,
  613 this email is sent to you as a reminder that you are participating in simulation $simulation_name that will start on $simulation_start_date. Here is the
  614 simulation description:
  615
  616 $simulation_description"
  617
556 618         acs_mail_lite::send \
557 619             -to_addr $email \
558 620             -from_addr [ad_system_owner] \
559 621             -subject $subject\
560 622             -body $body
  623        
  624         # Record that we sent email
  625         db_dml record_simulation_email {
  626             insert into sim_simulation_emails
  627                 (simulation_id, user_id, email_type, send_date)
  628              values
  629                 (:simulation_id, :user_id, 'reminder', current_timestamp)
561 630         }
562 631     }
  632 }
563 633
564 634 ad_proc -public simulation::template::start {
565 635      {-workflow_id:required}
566 636 } {
567 637     Make a simulation go live. Does enrollment and
568 638     casting and sets sim_type attribute to live_sim.
569 639
570 640     @author Peter Marklund
571 641 } {
572       # TODO (.25h): invoke this proc from a sweep
573  
574 642     simulation::template::get -workflow_id $workflow_id -array simulation
575 643
576 644     if { ![string equal $simulation(sim_type) "casting_sim"] } {
577 645         error "This simulation is in state $simulation(sim_type), it must be in 'casting_sim'"
578 646     }
579 647
580 648     db_transaction {
581 649         # Change sim_type to live_sim
582 650         set simulation_edit(sim_type) live_sim
583 651            
584 652         simulation::template::edit -workflow_id $workflow_id -array simulation_edit
585 653
586 654         simulation::template::autocast -workflow_id $workflow_id
587 655     }
  656
  657     # Notify users enrolled in the simulation
  658     db_foreach select_enrolled_users {
  659             select distinct cu.user_id,
  660                    cu.email,
  661                    cu.first_names || ' ' || cu.last_name as user_name
  662             from sim_party_sim_map spsm,
  663                  cc_users cu
  664             where spsm.simulation_id = :workflow_id
  665              and spsm.type = 'enrolled'
  666              and spsm.party_id = cu.user_id
  667     } {
  668         # TODO: check that link to enrollment page is correct
  669         set package_id [ad_conn package_id]
  670         set simplay_url \
  671             [export_vars -base "[ad_url][apm_package_url_from_id $package_id]/simplay/enroll" { email }]
  672         set subject "Simulation $simulation(pretty_name) has started"
  673         set body "Dear $user_name,
  674 Simulation $simulation(pretty_name) has now started. Please visit $simplay_url to participate. Thank you!"
  675
  676         acs_mail_lite::send \
  677             -to_addr $email \
  678             -from_addr [ad_system_owner] \
  679             -subject $subject\
  680             -body $body
588 681     }   
  682 }
589 683
590 684 ad_proc -public simulation::template::autocast {
591 685      {-workflow_id:required}
592 686 } {
593 687     Takes a mapped simulation template and converts it into a cast simulation
594 688     with simulation cases. This procedure expects to be called after enrollment is complete.
595 689
596 690     @author Peter Marklund
597 691 } {
598 692     # TODO (8h): also handle auto-casting when casting_type is not auto, but not all users self-cast
599 693
600 694     simulation::template::role_party_mappings \
601 695         -workflow_id $workflow_id \
602 696         -array roles
603 697
604 698     # Build user lists for each of the groups mapped to a role and calculate the total number
605 699     # of users in the groups
606 700     set total_users 0
607 701     foreach role_id [array names roles] {
608 702         array unset one_role
 
817 911 }
818 912
819 913 ad_proc -public simulation::template::get_inst_state {
820 914     -workflow_id:required
821 915 } {
822 916     Get information about which tab urls in the instantiation wizard
823 917     have been completed.
824 918
825 919     @return An array with the following keys (urls) and values either 0 or 1:
826 920
827 921     <ul>
828 922         simulation-edit
829 923         map-characters
830 924         map-tasks
831 925         simulation-participants
832 926         simulation-casting-3
833 927     </ul>
834 928 } {
835 929     simulation::template::get -workflow_id $workflow_id -array sim_template   
836 930    
837       # TODO (1.5h): Refactor this and the corresponding wizard.tcl/adp page
838       # What we really need to know is whether each step is complete
839       # They're all independent of each other, except for casting, which is dependent on participants.
840  
841 931     foreach tab [get_wizard_tabs] {
842 932         set tab_complete_p($tab) 0
843 933     }
844 934
845 935     switch $sim_template(sim_type) {
846 936         dev_sim {
847 937
848 938             # 1. Settings
849 939             if { ![empty_string_p $sim_template(case_start)] && ![empty_string_p $sim_template(send_start_note_date)] } {
850 940                 set tab_complete_p(simulation-edit) 1
851 941             }
852 942            
853 943             # 2. Roles
854 944             set role_empty_count [db_string role_empty_count {
855 945                 select count(*)
856 946                 from   sim_roles sr,
857 947                        workflow_roles wr
858 948                 where  sr.role_id = wr.role_id
859 949                 and    wr.workflow_id = :workflow_id
860 950                 and    character_id is null